package com.example.houtai.service.impl;

import com.example.houtai.entity.TbAlbum;
import com.example.houtai.entity.TbPremisesFacility;
import com.example.houtai.entity.TbPremisesInfo;
import com.example.houtai.entity.TbRelationPremisesProperty;
import com.example.houtai.mapper.*;
import com.example.houtai.service.ITbPremisesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.org.dto.Domains;
import com.org.dto.PreDto;
import com.org.dto.SubwayStatus;
import com.org.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼盘信息表 服务实现类
 * </p>
 *
 * @author 龚梦
 * @since 2021-03-26
 */
@Service
@Slf4j
public class TbPremisesInfoServiceImpl extends ServiceImpl<TbPremisesInfoMapper, TbPremisesInfo> implements ITbPremisesInfoService {

    @Autowired
    TbPremisesInfoMapper mapper;

    @Autowired
    TbRelationPremisesPropertyMapper promapper;

    @Autowired
    TbPremisesFacilityMapper tbPremisesFacilityMapper;

    @Autowired
    TbAlbumMapper tbAlbumMapper;

    @Override
    @Transactional
    public ResultEntity insert(PreDto preDto) {
        log.info("-------输入数据-------"+preDto);
        TbPremisesInfo tbPremisesInfo=new TbPremisesInfo();
        BeanUtils.copyProperties(preDto,tbPremisesInfo);
        tbPremisesInfo.setUpdateTime(LocalDateTime.now());

        String subway="";
        for(int i=0;i<preDto.getSubways().length;i++){
            if(i>0){
                subway+=",";
            }
            subway+=preDto.getSubways()[i];
        }
        log.info("合并地铁后："+subway);
        tbPremisesInfo.setSubway(subway);

        String sq="";
        for(int i=0;i<preDto.getSq().length;i++){
            if(i>0){
                sq+=",";
            }
            sq+=preDto.getSq()[i];
        }
        log.info("合并商圈后："+sq);
        tbPremisesInfo.setCommercialArea(sq);

        String buildType="";
        for(int i=0;i<preDto.getBuildingTypes().length;i++){
            if(i>0){
                buildType+=",";
            }
            buildType+=preDto.getBuildingTypes()[i];
        }
        log.info("合并建筑类型后："+buildType);
        tbPremisesInfo.setBuildingType(buildType);

        String premisesType="";
        for(int i=0;i<preDto.getPremisesTypes().length;i++){
            if(i>0){
                premisesType+=",";
            }
            premisesType+=preDto.getPremisesTypes()[i];
        }
        log.info("合并物业类型后："+premisesType);
        tbPremisesInfo.setPremisesType(premisesType);

        int i=mapper.insert(tbPremisesInfo);
        /**
         * 添加楼盘特色表
         */
        for(int j=0;j<preDto.getProperty().length;j++){
            TbRelationPremisesProperty tbRelationPremisesProperty=new TbRelationPremisesProperty();
            tbRelationPremisesProperty.setPremisesId(tbPremisesInfo.getId());
            tbRelationPremisesProperty.setPropertyId(preDto.getProperty()[j]);
            promapper.insert(tbRelationPremisesProperty);
        }


        /**
         * 周边配套
         */
        for (Domains domain : preDto.getDomains()) {
            TbPremisesFacility tbPremisesFacility=new TbPremisesFacility();
            tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
            tbPremisesFacility.setTitle(domain.getValue());
            tbPremisesFacility.setContent(domain.getContent());
            tbPremisesFacility.setType(domain.getType());
            tbPremisesFacilityMapper.insert(tbPremisesFacility);
        }

        /**
         * 交通情况
         */
        for (SubwayStatus subwayStatus : preDto.getSubwayStatus()) {
            TbPremisesFacility tbPremisesFacility=new TbPremisesFacility();
            tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
            tbPremisesFacility.setTitle(subwayStatus.getValue());
            tbPremisesFacility.setContent(subwayStatus.getContent());
            tbPremisesFacility.setType(subwayStatus.getType());
            tbPremisesFacilityMapper.insert(tbPremisesFacility);
        }


        /**
         * 图片 set hash gos 锁 sql底层 lok底层
         * cs算法 线程池(参数，作用)
         * springBoot流程 springMVC流程
         * IO AOP
         * 集群
         * MYSQL(索引)
         * 运用docker
         */

        for(int k=0;k<preDto.getImageUrl().size();k++){
            TbAlbum tbAlbum=new TbAlbum();
            tbAlbum.setPremisesId(tbPremisesInfo.getId());
            tbAlbum.setAlbumType(preDto.getImageUrl().get(k).getId());
            tbAlbum.setUrl(preDto.getImageUrl().get(k).getUrl());
            tbAlbum.setName(preDto.getImageUrl().get(k).getName());
            log.info("图片类型："+tbAlbum.getAlbumType()+"-----图片地址："+tbAlbum.getUrl());
            tbAlbumMapper.insert(tbAlbum);
        }

        log.info("新增结果："+i);
        if(i>0){
            return ResultEntity.ok("200","新增楼盘成功",null);
        }
        return ResultEntity.ok("500","新增楼盘失败",null);
    }

    @Override
    public ResultEntity listAll(PreDto preDto) {
        PageHelper.startPage(preDto.getPageNum(),preDto.getPageSize());
        List<TbPremisesInfo> list=mapper.listAll(preDto);
        PageInfo info = new PageInfo<>(list);
        return ResultEntity.ok("200","成功",info);
    }

    @Override
    public ResultEntity getlistAll(PreDto preDto) {
        PageHelper.startPage(preDto.getPageNum(),preDto.getPageSize());
        List<PreDto> list=mapper.getlistAll(preDto);
        PageInfo info = new PageInfo<>(list);
        return ResultEntity.ok("200","成功",info);
    }
}
